package cn.mrlong.base.permission

/**
 * 作者：BINGO
 * 时间：2021/04/08 10:01
 */

/**
 * 跳转设置页面
 */

import android.app.Activity
import android.content.ComponentName
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import cn.mrlong.base.utils.AppUtils


object JumpPermissionManagement {
    /**
     * Build.MANUFACTURER
     */
    private const val MANUFACTURER_HUAWEI = "Huawei" //华为
    private const val MANUFACTURER_MEIZU = "Meizu" //魅族
    private const val MANUFACTURER_XIAOMI = "Xiaomi" //小米
    private const val MANUFACTURER_OPPO = "OPPO"

    /**
     * 此函数可以自己定义
     * @param activity
     */
    fun goToSetting(activity: Activity) {
        try {
            val buildValue = Build.MANUFACTURER
            if (buildValue.equals(MANUFACTURER_HUAWEI, ignoreCase = true)) {
                Huawei(activity)
            } else if (buildValue.equals(MANUFACTURER_MEIZU, ignoreCase = true)) {
                Meizu(activity)
            } else if (buildValue.equals(MANUFACTURER_XIAOMI, ignoreCase = true)) {
                Xiaomi(activity)
            } else if (buildValue.equals(MANUFACTURER_OPPO, ignoreCase = true)) {
                OPPO(activity)
            } else {
                ApplicationInfo(activity)
            }
        } catch (e: Exception) {
            try {
                ApplicationInfo(activity)
            } catch (e: Exception) {
                SystemConfig(activity)
            }
        }
    }

    fun Huawei(activity: Activity) {
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.putExtra("packageName", AppUtils.getAppPackageName(activity))
        val comp = ComponentName(
            "com.huawei.systemmanager",
            "com.huawei.permissionmanager.ui.MainActivity"
        )
        intent.component = comp
        activity.startActivity(intent)
    }

    fun Meizu(activity: Activity) {
        val intent = Intent("com.meizu.safe.security.SHOW_APPSEC")
        intent.addCategory(Intent.CATEGORY_DEFAULT)
        intent.putExtra("packageName", AppUtils.getAppPackageName(activity))
        activity.startActivity(intent)
    }

    fun Xiaomi(activity: Activity) {
        val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
        val componentName = ComponentName(
            "com.miui.securitycenter",
            "com.miui.permcenter.permissions.AppPermissionsEditorActivity"
        )
        intent.component = componentName
        intent.putExtra("extra_pkgname", AppUtils.getAppPackageName(activity))
        activity.startActivity(intent)
    }


    fun OPPO(activity: Activity) {
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.putExtra("packageName", AppUtils.getAppPackageName(activity))
        val comp = ComponentName(
            "com.color.safecenter",
            "com.color.safecenter.permission.PermissionManagerActivity"
        )
        intent.component = comp
        activity.startActivity(intent)
    }


    /**
     * 应用信息界面
     * @param activity
     */
    fun ApplicationInfo(activity: Activity) {
        val localIntent = Intent()
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
            localIntent.data = Uri.fromParts("package", activity.packageName, null)
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.action = Intent.ACTION_VIEW
            localIntent.setClassName(
                "com.android.settings",
                "com.android.settings.InstalledAppDetails"
            )
            localIntent.putExtra("com.android.settings.ApplicationPkgName", activity.packageName)
        }
        activity.startActivity(localIntent)
    }

    /**
     * 系统设置界面
     * @param activity
     */
    fun SystemConfig(activity: Activity) {
        val intent = Intent(Settings.ACTION_SETTINGS)
        activity.startActivity(intent)
    }
}